package com.hnust.dao;

import com.hnust.domain.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface QuestionDao {
    //删除题目记录
    @Delete("delete from question_record where que_rec_id = #{queRecId}")
    @ResultType(Integer.class)
    Integer deleteQuestionRecord(String queRecId);
    //获取题目详情
    @Select("select que_id,que_typ_id,level,analysis,point,content from question where que_id = #{questionID}")
    @ResultType(Question.class)
    Question getQuestion(String questionID);
    //获取获取题目详情正确答案
    @Select("select `right`,content from alternative_answer where que_id = #{questionID}")
    @ResultType(AlternativeAnswer.class)
    List<AlternativeAnswer> getQuestionAnswer(String questionID);
    //获取做题记录列表
    @Select("select que_rec_id,stu_id,que_id,que_rec_type_id,`right`,time from question_record where stu_id = #{studentID} and que_rec_type_id= #{recordType} limit #{offset},#{limit}")
    @ResultType(QuestionRecord.class)
    List<QuestionRecord> getRecordsList(String studentID,Integer recordType,Integer limit,Integer offset);
    //获取做题记录列表其余信息
    @Select("select level,content from question where que_id = #{questionID}")
    @ResultType(Question.class)
    Question getRecordsQuestionList(String questionID);
    //获取答题记录总量
    @Select("select count(*) from question_record where stu_id = #{studentID} and que_rec_type_id= #{recordType}")
    @ResultType(Integer.class)
    Integer getRecordsListCount(String studentID,Integer recordType);

    //获取各类题型题量
    @Select("select count(*) from question where cha_id=#{chapterId} and que_typ_id=#{queTypId}")
    @ResultType(Integer.class)
    Integer  getQueTypeCount(String chapterId, String queTypId);

    //根据难度获取各类题型题量
    @Select("select count(*) from question where cha_id=#{chapterId} and que_typ_id=#{queTypId} and level=#{level}")
    @ResultType(Integer.class)
    Integer getQueTypeCountByLevel(String chapterId, String queTypId,Integer level);

    //获取试卷信息
    @Select("select * from question where cha_id=#{chapterId} and que_typ_id=#{queTypId} and level=#{level} order by rand() limit #{count}")
    @ResultType(Question.class)
    List<Question> getQuestionByInfo(String chapterId,String queTypId,Integer level,Integer count);

    //无难度信息获取试卷信息
    @Select("select * from question where cha_id=#{chapterId} and que_typ_id=#{queTypId} order by rand() limit #{count}")
    @ResultType(Question.class)
    List<Question> getQuestionByInfoNoLevel(String chapterId,String queTypId,Integer count);

    //根据课程id获取题目
    @Select("select * from question where cou_id=#{courseId}")
    @ResultType(Question.class)
    List<Question> getQuestionsByCourseId(String courseId);

    //获取答案
    @Select("select * from alternative_answer where que_id=#{questionId}")
    @ResultType(AlternativeAnswer.class)
    List<AlternativeAnswer> getAlternativeAnswer(String questionId);

    //根据课程id获取题目总量
    @SelectProvider(type = getQuestionsCountByCourseId.class,method = "getQuestionsCountByCourseId")
    @ResultType(Integer.class)
    public Integer getQuestionsCountByCourseId(String courseId,String chapterId);
    class getQuestionsCountByCourseId{
        public String getQuestionsCountByCourseId(String courseId,String chapterId){
            String sql="select count(*) from question where cou_id='"+courseId+"'";
            if(chapterId!=null){
                sql+=" and cha_id='"+chapterId+"'";
            }
            return sql;
        }
    }


    @SelectProvider(type = getQuestionsByCourseIdAndOffset.class,method = "getQuestionsByCourseIdAndOffset")
    @ResultType(Question.class)
    public List<Question> getQuestionsByCourseIdAndOffset(String courseId,long offset,String chapterId);
    class getQuestionsByCourseIdAndOffset{
        public String getQuestionsByCourseIdAndOffset(String courseId,long offset,String chapterId){
            String sql="select * from question where cou_id='"+courseId+"'";
            if(chapterId!=null){
                sql+=" and cha_id='"+chapterId+"'";
            }
            sql+=" limit 20 offset "+offset;
            return sql;
        }
    }

    //根据章节ID获取章节
    @Select("select * from chapter where cha_id=#{cha_id}")
    @ResultType(Chapter.class)
    Chapter getChapterById(String cha_id);

    //根据章节编号获取子章节信息
    @SelectProvider(type = getChildrenChapterIDs.class,method = "getChildrenChapterIDs")
    @ResultType(String.class)
    public List<String> getChildrenChapterIDs(String courseId, Integer chapter, Integer first, Integer second, Integer third);
    class getChildrenChapterIDs{
        public String getChildrenChapterIDs(String courseId, Integer chapter, Integer first, Integer second, Integer third){
            String sql = "select cha_id from chapter where cou_id='"+courseId+" and chapter="+chapter;
            if(first!=0){
                sql+=" and first="+first;
            }
            if(second!=0){
                sql+=" and second="+second;
            }
            if(third!=0){
                sql+=" and third="+third;
            }
            return sql;
        }
    }

}
